package huanyuxingcheng.nonameinstallhelper;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DecadeCardInstallActivity extends BaseActivity implements View.OnClickListener {

    private Spinner skinSpinner;
    /**
     * 安装
     */
    private Button installButton;

    private File file;

    private List<File> files = new ArrayList<>();

    private BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return files.size();
        }

        @Override
        public Object getItem(int i) {
            return files.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view == null){
                view = View.inflate(DecadeCardInstallActivity.this,R.layout.spinner_text_item,null);
            }
            TextView textView = view.findViewById(R.id.item_text);
            File file = files.get(i);
            textView.setText(file.getName());
            return view;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_decade_card_install);
        initView();
        File qqFile = NonameUtils.getQQDownloadDirectory();
        files.addAll(NonameUtils.fileFilter(qqFile, new NonameUtils.Filter<File>() {
            @Override
            public boolean filter(File file) {
                return file.getName().contains("安装器兼容")
                        && file.getName().contains("十周年卡牌")
                        && file.getName().endsWith(".zip");
            }
        }));
        skinSpinner.setAdapter(adapter);
        skinSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if(i >= 0 && files.size() > 0 && files.size() > i) {
                    file = files.get(i);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    private void initView() {
        skinSpinner = findViewById(R.id.skin_spinner);
        installButton = findViewById(R.id.install_button);
        installButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.install_button:
                if(file == null){
                    showShortToast("未选择文件");
                    return;
                }
                Intent intent = new Intent(this,ZipActivity.class);
                intent.putExtra("fromPath",file.getAbsolutePath());
                intent.putExtra("toPath",NonameUtils.getNonameDecadeCardDirectory().getAbsolutePath());
                startActivity(intent);
                break;
        }
    }
}
